<?php

class BuyController extends Zend_Controller_Action
{

    public function init()
    {	

        $core = new Model_Core();
        $this->view->getcontroller = Zend_Controller_Front::getInstance()->getRequest()->getControllerName();
        $this->view->getaction = Zend_Controller_Front::getInstance()->getRequest()->getActionName();
		
        $city_arr = $this->getCityarr();
        $this->view->putcity = $city_arr[city_name];
        $this->view->putcity_id = $city_arr[city_id];
        //城市列表   用于底部 切换城市 已使用AJAX
        $all_city = new Model_City();
        $this->view->all_city = $all_city->getAllCity();
        
        $carmodel = new Model_CarModel();
        $this->view->allbrand = $carmodel->getAllBrand();
        
        //USE AJAX TO LOGIN ACCOUNT?
        if ($core->isLogin()){
            $this->view->member = $core->isLogin();
        }
        $this->view->ref = urlencode("http://".$_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"]);
        
        $this->view->ismoblie = $core->is_mobile();
        
        $this->view->nowtitle = "我要买车";
        $this->_helper->layout->setLayout('buy');
    }
    

    
    //所有城市
    protected function getCityarr(){
        $core = new Model_Core();
        $city_id = intval($this->_request->getParam('city_id'));
        $ip = $_SERVER['REMOTE_ADDR'];
        $city_arr = $core->getNowCity($city_id, $ip);
        return $city_arr;
    }
	
	protected function getTime(){
	    return time();
	}
    
    public function indexAction(){

		$city_arr = $this->getCityarr();
		//车型
		$carmodel = new Model_CarModel();
		//$this->view->allbrand = $carmodel->getAllBrand();

		$brand_id = intval($this->_request->getParam('brand'));
		$this->view->brand_id = $brand_id;
		
		if ($brand_id) {
		    $model_id = intval($this->_request->getParam('model'));
		    $this->view->putmodel = $carmodel->getModel($brand_id);
		    $this->view->model_id = $model_id;
		}
		
		$core = new Model_Core();
		$config = $core->getConfig();
		$this->view->putconfig = $config;
		//车价
		$low_price = intval($this->_request->getParam('low_price'));
		$this->view->low_price = $low_price;
		$high_price = intval($this->_request->getParam('high_price'));
		$this->view->high_price = $high_price;
		
		//车龄
		$low_age = intval($this->_request->getParam('low_age'));
		$this->view->low_age = $low_age;
		$high_age = intval($this->_request->getParam('high_age'));
		$this->view->high_age = $high_age;
		
		//关键词
		$query = addslashes(trim($this->_request->getParam('query')));
		$this->view->query = $query;
		
		//NAV 输出
		$nav = "";
		$nav .= "<li><span class=\"divider\">/</span><a href=\"".$this->view->url(array('controller'=>'buy'),null,true)."\">我要买车</a> </li>";
		$nav .= "<li><span class=\"divider\">/</span><a href=\"".$this->view->url(array('controller'=>'buy','city_id'=>$city_arr[city_id]),null,true)."\">$city_arr[city_name]二手车</a> </li>";
		
		//SEO
		$seotitle = "【";
		
		//IS SELECTED
		$selected = array();
		if ($query){
			$selected['query'] = "关键词：".htmlspecialchars($query);
			$seotitle .= "关键词：$query ";
		}
		if ($brand_id){
			$selected['brand'] = $carmodel->getModelName($brand_id);
			$nav .= "<li><span class=\"divider\">/</span><a href=\"".$this->view->url(array('controller'=>'buy','brand'=>$brand_id),null,true)."\">".$selected['brand']['brand']."</a> </li>";
			$seotitle .= $selected['brand']['brand']." ";
		}
		if ($model_id){
			$selected['model'] = $carmodel->getModelName($model_id);
			$nav .= "<li><span class=\"divider\">/</span><a href=\"".$this->view->url(array('controller'=>'buy','brand'=>$brand_id,'model'=>$model_id),null,true)."\">".$selected['model']['model']."</a> </li>";
			$seotitle .= $selected['model']['model']." ";
		}
		//print_r($config);
		foreach ($config[price] as $k=>$value){
		    if ($value[low] == $low_price && $value[high] == $high_price && $value[name] != "不限") {
		    	$selected['price'] = $value[name];
		    	$seotitle .= $value[name]." ";
		    }
		 }
		 foreach ($config[age] as $k=>$value){
		    if ($value[low] == $low_age && $value[high] == $high_age && $value[name] != "不限") {
		    	$selected['age'] = $value[name];
		    	$seotitle .= $value[name]." ";
		    }  
		}
		//$selected['price'] = 
		$this->view->selected = $selected;
		//类型
		$type = intval($this->_request->getParam('type', 0));
		$this->view->type = $type;
		//排序
		$order = trim($this->_request->getParam('order', 'default'));
		$this->view->order = $order;
		
		//有图及在售
		$is_pic = intval($this->_request->getParam('is_pic', 0));
		$this->view->is_pic = $is_pic;
		$on_sale = intval($this->_request->getParam('on_sale', 0));
		$this->view->on_sale = $on_sale;
		
		$page = intval($this->_request->getParam('page', 1));
		//输出车源列表
		$sale_arr = new Model_CarSale();
		$list = $sale_arr->getList($page, $this->view->url(array('city_id'=>$city_arr[city_id],'page'=>NULL))."/page/", $city_arr[city_id], $brand_id, $model_id, $low_price, $high_price, $low_age, $high_age, $is_pic, $on_sale, $query, $type, $order);
		$this->view->list = $list;
		
		
		$this->view->nav = $nav;
		
		$seotitle .= $city_arr[city_name]."二手车】";
		
		//KEYWORDS
		$seokeywords = str_ireplace("【", "", $seotitle);
		$seokeywords = str_ireplace("】", "", $seokeywords);
		$seokeywords = str_ireplace(" ", ",", $seokeywords);
        // action body
        $this->view->headTitle($seotitle." 我要买车", 'PREPEND');
        $this->view->headMeta()->setName('keywords', $seokeywords);
        $this->view->headMeta()->setName('description', $seotitle.", 淘车乐二手车为您提供最真实的二手车信息服务！");
    }
    
    public function viewAction(){
		$city_arr = $this->getCityarr();
		
		$cid = intval($this->_request->getParam('cid'));
		
		$carsale = new Model_CarSale();
		$row = $carsale->getView($cid);
		
		$this->view->row = $row;
		$this->view->cid = $cid;
		
		//同类车型 同价位车型
		$samelprice = $row[price] - 5;
		$samehprice = $row[price] + 5;
		$sameprice = $carsale->getSameList($city_arr[city_id],"", $samelprice, $samehprice);
		$samebrand = $carsale->getSameList($city_arr[city_id], $row[brand_id],"","");

		$this->view->sameprice = $sameprice;
		$this->view->samebrand = $samebrand;
		
		//上一个 下一个
		$re_res = $carsale->getViewName($cid - 1);
		$next_res = $carsale->getViewName($cid + 1);
		$this->view->re_res = $re_res;
		$this->view->next_res = $next_res;
		
		//NAV
		$nav = "<li><span class=\"divider\">/</span><a href=\"".$this->view->url(array('controller'=>'buy'),null,true)."\">我要买车</a> </li>";
		$nav .= "<li><span class=\"divider\">/</span><a href=\"".$this->view->url(array('controller'=>'buy','city_id'=>$city_arr[city_id]),null,true)."\">$city_arr[city_name]二手车</a> </li>";
		$nav .= "<li><span class=\"divider\">/</span><a href=\"".$this->view->url(array('controller'=>'buy','action'=>'view','cid'=>$cid),null,true)."\">".$row[title]."</a> </li>";
 		$this->view->nav = $nav;
 		
 		
 		//详细配置参数
 		/* AJAX LOAD
 		if ($row[name_id] != -1){
 		    $detail = new Model_CarDetailtb();
 		    $datailres = $detail->getAlldetail($row[name_id]);
 		    $this->view->detailres = $datailres;
 		}*/
 		if ($row[is_app] == 3) {
 			$this->_forward('error');
 		}
 		//APP = 4 审核
 		if (!$row || $row[is_app] == 4){
 		    $this->_forward('error');
 		}

 		$seokeywords = str_ireplace(" ", ",", $row[title]);
        $this->view->headTitle("$row[name] $row[title] $row[price]万元", 'PREPEND');
        $this->view->headMeta()->setName('keywords', "$seokeywords,$city_arr[city_name]二手车");
        $this->view->headMeta()->setName('description', "$row[title] $city_arr[city_name]二手车车源出售");
    }
    
    //车辆对比
    public function contrastAction() {
    	
        $cids = trim($this->_request->getParam('cids'));
        $cids = explode(",", $cids);
        
        $sale = new Model_CarSale();
        $data = $sale->getContrastView($cids);
        
        $this->view->data = $data;
                
        $this->view->nowtitle = "车辆对比";
                
        $this->_helper->layout->setLayout('sale');
        
        $this->view->headTitle("车辆对比工具", 'PREPEND');
        $this->view->headMeta()->setName('keywords', "车辆对比工具");
        $this->view->headMeta()->setName('description', "淘车乐车辆对比工具");
    }
    
    public function errorAction() {
    	
        //删除 未知ID 等
    }

}